/*
 * @Description: 
 * @Author: changqing
 * @Date: 2021-08-16 14:58:26
 * @LastEditTime: 2021-08-16 15:18:21
 * @LastEditors: changqing
 * @Usage: 
 */

//通过ES6iterater protocol协议来实现

const createIterator = (data) => {
  let nextIndex = 0;

  return {
   [Symbol.iterator]: () => {
   	if(nextIndex < data.length){
    		return {
    			value: data[nextIndex++],
          done: false
    		}
    	} else {
    		 return {
         	done: true,
         	value:void 0
         };
    	}
   }
  };
};


const arr = [1, 2, 3];
const iterator = arr[Symbol.iterator]();
iterator.next();
// { value: 1, done: false }
iterator.next();
// { value: 2, done: false }
iterator.next();
// { value: 3, done: false }
iterator.next();
// { value: undefined, done: true }